package com.skt.common_hr.common.memberselector
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class MemberSelectorProxy extends CommonProxy
	{
		public static const NAME:String = "MemberSelectorProxy";
		
		public static const LOAD_ORGLIST_COMPLETE:String 	= "loadOrgListComplete";
		public static const LOAD_ORGLIST_FAILED:String 		= "loadOrgListFailed";
		public static const LOAD_MEMBERLIST_COMPLETE:String	= "loadMemberListComplete";
		public static const LOAD_MEMBERLIST_FAILED:String	= "loadMemberListFailed";		
		public static const LOAD_SEARCH_COMPLETE:String		= "loadSearchComplete";
		public static const LOAD_SEARCH_FAILED:String		= "loadSearchFailed";		
		
		public var orgList:XMLList;
		public var orgRoot:XML;
		public var memberList:ArrayCollection;
		public var pernr:String;			//사번
		public var name:String;				//이름
		public var stext:String;			//부서명(상위조직명 포함)
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function MemberSelectorProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function loadOrg(pernr:String, auth:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_AUTH"] = auth;
			
			loadData("Z_HR_0015", param, resultOrg, faultOrg); 
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function loadMember(orgeh:String, keyword:String=""):void
		{
			var param:Object = new Object();
			param["I_ORGEH"] = orgeh;
			param["I_KEYWORD"] = keyword;
			
			loadData("Z_HR_0016", param, resultMember, faultMember); 
		}
		
		//성명 검색 ( 성명검색시 결과 메시지 출력 떄문에 펑션 분리 )
		public function searchName(orgeh:String, keyword:String):void
		{
			var param:Object = new Object();
			//param["I_ORGEH"] = orgeh;
			param["I_PERNR"] = Variables.myPernr;
			param["I_KEYWORD"] = keyword;
			
			loadData("Z_HR_0016", param, resultSearch, faultSearch); 
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultOrg(event:ResultEvent):void
		{
			if(retCode==0) {  
				orgList = parseOrgList(event.result.RTAB);
				facade.sendNotification(LOAD_ORGLIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_ORGLIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultOrg(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_ORGLIST_FAILED, this);
		}
		
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultMember(event:ResultEvent):void
		{
			if(retCode==0) {  
				memberList = parseMemberList(event.result.RTAB);
				facade.sendNotification(LOAD_MEMBERLIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_MEMBERLIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultMember(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_MEMBERLIST_FAILED, this);
		}
		
		//성명검색 성공
		public function resultSearch(event:ResultEvent):void
		{
			if(retCode==0) {  
				memberList = parseMemberList(event.result.RTAB);
				facade.sendNotification(LOAD_SEARCH_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_SEARCH_FAILED, this);
			}
		}
		
		//성명검색 실패
		private function faultSearch(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_SEARCH_FAILED, this);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseOrgList(retObj:ArrayCollection):XMLList
		{
			var prevArr:Array = new Array();
			if(retObj.length>0) {
				orgRoot = <node label={retObj[0].STEXT} objid={retObj[0].OBJID} level={retObj[0].LEVEL} parent={retObj[0].UPPERORG}/>;
				prevArr[orgRoot.@level] = orgRoot;
							
				for(var i:int=1; i<retObj.length; i++)
				{
					var rec:Object = retObj[i];
					var node:XML = <node label={rec.STEXT} objid={rec.OBJID} level={rec.LEVEL} parent={rec.UPPERORG}/>;
					if(prevArr[parseInt(rec.LEVEL)-1]!=null) {
						XML(prevArr[parseInt(rec.LEVEL)-1]).appendChild(node);
					}
					prevArr[node.@level] = node;
				}
			}
			else {
				orgRoot = <node label={"No Data"} objid={"0000"} level={"0"} parent={"0000"}/>;
			}
			return new XMLList(orgRoot);
		}

		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseMemberList(retObj:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in retObj)
			{
				var vo:MemberVO = new MemberVO();
				vo.ORGEH 		= rec.ORGEH;
				vo.STEXT 		= rec.STEXT;
				vo.PERNR 		= StringUtil.trimFront(rec.PERNR,"0");
				vo.ENAME 		= rec.ENAME;
				vo.PLANST 		= rec.PLANST;
				vo.PTEXT 		= rec.PTEXT;
				vo.STELLT 		= rec.STELLT;
				vo.JWTXT 		= rec.JWTXT;
				vo.TEL_OFFICE 	= rec.TEL_OFFICE;
				vo.TEL_MOBILE1	= rec.TEL_MOBILE1;
				vo.TEL_MOBILE2 	= rec.TEL_MOBILE2;
				vo.WKADDR		= rec.WKADDR;
				vo.PHOTO_URL 	= rec.PHOTO_URL;
				if(vo.PHOTO_URL.length<1) {
					vo.PHOTO_URL = "assets/images/noimage.swf";
				}
				vo.BAND 		= rec.BAND;
				vo.PERSK 		= rec.PERSK;
				list.addItem(vo);
			}
			return list;
		}
	}
}